#define _CRT_SECURE_NO_WARNINGS 1
#include"stu_message.h"
//菜单
void menu()
{
	printf("***********************************************\n");
	printf("************1.Add         2.Del****************\n");
	printf("************3.Search      4.Modify*************\n");
	printf("************5.Display     6.Empty**************\n");
	printf("************7.Sort_num   0.Exit***************\n");
	printf("***********************************************\n");
}
//打印标题
void Print_head()
{
	char* emtpy = " ";
	printf("学号%-9s姓名%-8s年龄%-2s性别%-3s出生日期%-12s地址%-31s电话%-11sE-mail%-21s\n", emtpy, emtpy, emtpy, emtpy, emtpy, emtpy, emtpy, emtpy);
}

//动态初始化学生表
void Init_Stu(STU* ps)
{
	assert(ps);
	ps->sz = 0;
	ps->capacity = INITIAL_MAN;
	ps->data = (Message*)malloc(ps->capacity * sizeof(Message));
	if (ps->data == NULL)
	{
		perror("Init_Con::malloc");
		return;
	}
	memset(ps->data, 0, ps->capacity * sizeof(Message));
	//加载文件信息到学生表
	Loadstu(ps);
}
//扩容
void Expasion_capacity(STU* ps)
{
	if (ps->sz == ps->capacity)
	{
		Message* pa = (Message*)realloc(ps->data, (ps->capacity + 5) * sizeof(Message));
		if (pa != NULL)
		{
			ps->data = pa;
		}
		else
		{
			perror("Expasion_capacity::realloc");
			return;
		}
		ps->capacity += 5;
		printf("扩容成功\n");
	}
}
//增添数据
//void Add_message(STU* ps)
//{
//	assert(ps);
//	if (ps->sz == ps->capacity)
//	{
//		Expasion_capacity(ps);
//	}
//	printf("输入学生学号>");
//	scanf("%s", ps->data[ps->sz].number);
//	printf("输入学生姓名>");
//	scanf("%s", ps->data[ps->sz].name);
//	printf("输入学生年龄>");
//	scanf("%d", &ps->data[ps->sz].age);
//	if (ps->data[ps->sz].age > 50 || ps->data[ps->sz].age < 6)
//	{
//		printf("年龄不符合学生区域\n");
//		exit(-1);
//	}
//	printf("输入学生性别>");
//	scanf("%s", ps->data[ps->sz].sex);
//	printf("输入学生出生日期>");
//	scanf("%s", ps->data[ps->sz].birthday);
//	printf("输入学生地址>");
//	scanf("%s", ps->data[ps->sz].address);
//	printf("输入学生电话>");
//	scanf("%s", ps->data[ps->sz].tel);
//	if (strlen(ps->data[ps->sz].tel) != 11)
//	{
//		printf("学生手机号不符合11位，输入有误\n");
//		exit(-1);
//	}
//	printf("输入学生E-mail>");
//	scanf("%s", ps->data[ps->sz].E_mail);
//	ps->sz++;
//}
void Add_message(STU* ps)
{
	assert(ps);
	if (ps->sz == ps->capacity)
	{
		Expasion_capacity(ps);
	}
s1:	printf("输入学生学号>");
	scanf("%s", ps->data[ps->sz].number);

	for (int i = 0; i < ps->sz; i++)
	{
		if (0 == strcmp(ps->data[ps->sz].number, ps->data[i].number))
		{
			printf("学号已存在，请重新输入：\n");
			goto s1;
		}
	}
	printf("输入学生姓名>");
	scanf("%s", ps->data[ps->sz].name);
	printf("输入学生年龄>");
	scanf("%d", &ps->data[ps->sz].age);
	if (ps->data[ps->sz].age > 50 || ps->data[ps->sz].age < 15)
	{
		printf("年龄不符合，请重新输入：\n");
		printf("输入学生年龄>");
		scanf("%d", &ps->data[ps->sz].age);
	}
	printf("输入学生性别>");
	scanf("%s", ps->data[ps->sz].sex);
	printf("输入学生出生日期>");
	scanf("%s", ps->data[ps->sz].birthday);
	printf("输入学生地址>");
	scanf("%s", ps->data[ps->sz].address);
	printf("输入学生电话>");
	scanf("%s", ps->data[ps->sz].tel);
	if (strlen(ps->data[ps->sz].tel) != 11)
	{
		printf("学生手机号不符合11位，输入有误\n");
		exit(-1);
	}
	printf("输入学生E-mail>");
	scanf("%s", ps->data[ps->sz].E_mail);
	ps->sz++;
}
//打印单个学生信息
void Print_pos(STU* ps, int index)
{
	printf("%-13s%-12s%-6d%-7s%-16s%-35s%-15s%-25s\n", ps->data[index].number, ps->data[index].name, ps->data[index].age,
		ps->data[index].sex, ps->data[index].birthday, ps->data[index].address, ps->data[index].tel, ps->data[index].E_mail);
}
//显示学生表
void Display_message(const STU* ps)
{
	assert(ps);
	Print_head();
	for (int i = 0; i < ps->sz; i++)
	{
		Print_pos(ps, i);
	}
	printf("\n");
}

//删除学生
void Delete_message(STU* ps)
{
	assert(ps);
	printf("输入待删除学生学号>");
	char arr[20] = { 0 };
	scanf("%s", arr);
	int i = 0;
	//查找到下标
	for (i = 0; i < ps->sz; i++)
	{
		if (0 == strcmp(arr, ps->data[i].number))
		{
			break;
		}
	}
	//覆盖
	for (i; i < ps->sz - 1; i++)
	{
		ps->data[i] = ps->data[i + 1];
	}
	if (ps->sz == 0 || i == ps->sz)
	{
		printf("系统内无该学生，无法执行删除\n");
		return;
	}
	ps->sz--;
	printf("删除学生成功\n");
}
//查找学生
int Search_message(const STU* ps)
{
	assert(ps);
	if (ps->sz == 0)
	{
		printf("系统内无该学生，无法执行查找\n");
		return -1;
	}
	printf("输入待查找学生学号>");
	char arr[20] = { 0 };
	scanf("%s", arr);
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (0 == strcmp(arr, ps->data[i].number))
		{
			Print_head();
			Print_pos(ps, i);
			return i;
		}
	}
	printf("系统内无此学生\n");
	return -1;
}
//修改选项
void Modify_menu()
{
	printf("============\n");
	printf("===1.姓名===\n");
	printf("===2.年龄===\n");
	printf("===3.性别===\n");
	printf("===4.出生日期===\n");
	printf("===5.地址===\n");
	printf("===6.电话===\n");
	printf("===7.E-mail===\n");
	printf("===0.退出===\n");
	printf("============\n");
}
//修改联系人姓名
void modify_name(STU* ps, int index)
{
	assert(ps);
	char str[NAME] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].name, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);
}
//修改联系人出生日期
void modify_birthday(STU* ps, int index)
{
	assert(ps);
	char str[BIR] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].birthday, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);
}
//修改联系人邮箱
void modify_E_mail(STU* ps, int index)
{
	assert(ps);
	char str[MAIL] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].E_mail, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);
}
//修改联系人性别
void modify_sex(STU* ps, int index)
{
	assert(ps);
	char str[SEX] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].sex, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);


}
//修改联系人年龄
void modify_age(STU* ps, int index)
{
	assert(ps);
	int newage = 0;
	printf("修改为>");
	scanf("%d", &newage);
	ps->data[index].age = newage;
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);


}
//修改联系人电话
void modify_tel(STU* ps, int index)
{
	assert(ps);
	char str[TEL] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].tel, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);


}
//修改联系人地址
void modify_address(STU* ps, int index)
{
	assert(ps);
	char str[ADDRESS] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].address, str);
	printf("修改成功,修改后信息如下:\n");
	Print_pos(ps, index);

}
//修改联系人信息
void Modify_message(STU* ps)
{
	assert(ps);
	int index = Search_message(ps);
	if (index == -1)
	{
		exit(-1);
	}
	Modify_menu();
	int num = 0;
	printf("请选择修改项>");
	do {
		scanf("%d", &num);
		switch (num)
		{
		case 0:
			break;
		case 1:
			modify_name(ps, index);
			break;
		case 2:
			modify_age(ps, index);
			break;
		case 3:
			modify_sex(ps, index);
			break;
		case 4:
			modify_birthday(ps, index);
			break;
		case 5:
			modify_address(ps, index);
			break;
		case 6:
			modify_tel(ps, index);
			break;
		case 7:
			modify_E_mail(ps, index);
		default:
			printf("选择错误\n");
			break;
		}
	} while (num);
}

//清空学生
void Empty_book(STU* ps)
{
	assert(ps);
	ps->sz = 0;
	printf("通讯录已清空\n");
}

//比较函数
int cmp(const void* elm1, const void* elm2)
{
	return (strcmp(((Message*)elm1)->number, ((Message*)elm2)->number));
}

//按学号排序
STU* sort_number(STU* ps)
{
	assert(ps);
	qsort(ps->data, ps->sz, sizeof(Message), cmp);
	printf("排序完成\n");
	return ps;
}

//销毁通讯录
void Release_stu(STU* ps)
{
	free(ps->data);
	ps->data = NULL;
	ps->sz = 0;
	ps->capacity = 0;
}

//文件加载
void Loadstu(STU* ps)
{
	FILE* fp = fopen("student.txt", "rb");
	if (fp == NULL)
	{
		perror("LoadStu::fopen");
		return;
	}
	Message tmp = { 0 };
	while (fread(&tmp, sizeof(Message), 1, fp))
	{
		Expasion_capacity(ps);
		ps->data[ps->sz] = tmp;
		ps->sz++;
	}
	fclose(fp);
	fp = NULL;
}

//保存学生表
void Save_stu(STU* ps)
{
	FILE* fp = fopen("student.txt", "wb");
	if (fp == NULL)
	{
		perror("Save_stu::fopen:");
		return;
	}
	int i = 0;
	for (int i = 0; i < ps->sz; i++)
	{
		fwrite(ps->data + i, sizeof(Message), 1, fp);
	}
	fclose(fp);
	fp = NULL;
}